package com.enqualcomm.kids.component;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.enqualcomm.kids.BuildConfig;

public class MyContentProvider extends ContentProvider {

    private static final String path = "content://push."+ BuildConfig.APPLICATION_ID;
    public static final Uri MSG_URI = Uri.parse(path);

    public static final Uri AUTHPASS_URI = Uri.parse(path+"/authpass");
    public static final Uri AUTHPHONE_URI = Uri.parse(path+"/authphone");
    public static final Uri OWNERCHANGE_URI = Uri.parse(path+"/ownerchange");
    public static final Uri KICKUSER_URI = Uri.parse(path+"/kickuser");

    public static final Uri ISCHAGER_URI = Uri.parse(path+"/ischager");
    public static final Uri ISLOWBAT_URI = Uri.parse(path+"/islowbat");
    public static final Uri PUSHFENCING_URI = Uri.parse(path+"/pushfencing");
    public static final Uri SOSMSG_URI = Uri.parse(path+"/sosmsg");

    public static final Uri CHAT_URI = Uri.parse(path+"/chat");

    public static final Uri DETACH_NOTICE_URI = Uri.parse(path+"/detachnotice");

    private static UriMatcher matcher = new UriMatcher(-1);
    static{

        String path = "push."+ BuildConfig.APPLICATION_ID;
        matcher.addURI(path, "authpass", 1);
        matcher.addURI(path, "authphone", 2);
        matcher.addURI(path, "ownerchange", 3);
        matcher.addURI(path, "kickuser", 8);
        matcher.addURI(path, "ischager", 4);
        matcher.addURI(path, "islowbat", 5);
        matcher.addURI(path, "pushfencing", 6);
        matcher.addURI(path, "sosmsg", 7);
        matcher.addURI(path,"chat",9);
        matcher.addURI(path,"detachnotice",10);
    }

    private SQLiteDatabase db;

    @Override
    public boolean onCreate() {
        db = MySqlitOpenHelper.getInstance(getContext()).getWritableDatabase();
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;
        int code = matcher.match(uri);
        switch (code) {
            case 1:
                cursor = db.query("authpass", projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case 2:
                cursor = db.query("authphone", projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case 3:
                cursor = db.query("ownerchange", projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case 4:
                cursor = db.query("ischager", projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case 5:
                cursor = db.query("islowbat", projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case 6:
                cursor = db.query("pushfencing", projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case 7:
                cursor = db.query("sosmsg", projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case 8:
                cursor = db.query("kickuser", projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case 10:
                cursor = db.query("detachnotice", projection, selection, selectionArgs, null, null, sortOrder);
                break;
            default:
                break;
        }
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int code = matcher.match(uri);
        switch (code) {
            case 1:
                db.insert("authpass", "", values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 2:
                db.insert("authphone", "", values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 3:
                db.insert("ownerchange", "", values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 4:
                db.insert("ischager", "", values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 5:
                db.insert("islowbat", "", values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 6:
                db.insert("pushfencing", "", values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 7:
                db.insert("sosmsg", "", values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 8:
                db.insert("kickuser", "", values);
                break;
            case 9:
                db.insert("chat","",values);//聊天消息只有插入走这里，其他都直接用db
                getContext().getContentResolver().notifyChange(CHAT_URI, null);
                break;
            case 10:
                db.insert("detachnotice", "", values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            default:
                break;
        }
        return uri;
    }


    @Override
    public int bulkInsert(Uri uri, ContentValues[] values) {
        int code = matcher.match(uri);
        String tableName = null;
        int i = 0;
        switch (code) {
            case 1:
                tableName = "authpass";
                i= bulkInsertLocale(tableName, values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 2:
                tableName = "authphone";
                i= bulkInsertLocale(tableName, values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 3:
                tableName = "ownerchange";
                i= bulkInsertLocale(tableName, values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 4:
                tableName = "ischager";
                i= bulkInsertLocale(tableName, values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 5:
                tableName = "islowbat";
                i= bulkInsertLocale(tableName, values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 6:
                tableName = "pushfencing";
                i= bulkInsertLocale(tableName, values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 7:
                tableName = "sosmsg";
                i= bulkInsertLocale(tableName, values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 8:
                tableName = "kickuser";
                i= bulkInsertLocale(tableName, values);
                break;
            case 9:
                tableName = "chat";//聊天消息只有插入走这里，其他都直接用db
                i= bulkInsertLocale(tableName, values);
                getContext().getContentResolver().notifyChange(CHAT_URI, null);
                break;
            case 10:
                tableName = "detachnotice";
                i= bulkInsertLocale(tableName, values);
                getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            default:
                break;
        }

        return i;

    }
    private int bulkInsertLocale(String tableName,ContentValues[] values){
        if (tableName!=null) {
            db.beginTransaction();
            try {
                int count = values.length;
                for (int i = 0; i < count; i++) {
                    if (db.insert(tableName, "", values[i]) < 0) {
                        return 0;//如果插入不成功
                    }
                }
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
            return values.length;
        }else {
            return 0;
        }
    }




    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int code = matcher.match(uri);
        switch (code) {
            case 1:
                db.delete("authpass", selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 2:
                db.delete("authphone", selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 3:
                db.delete("ownerchange", selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 4:
                db.delete("ischager", selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 5:
                db.delete("islowbat", selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 6:
                db.delete("pushfencing", selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 7:
                db.delete("sosmsg", selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 8:
                db.delete("kickuser", selection, selectionArgs);
                break;
            case 10:
                db.delete("detachnotice", selection, selectionArgs);
                break;
            default:

                break;
        }
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int code = matcher.match(uri);
        switch (code) {
            case 1:
                db.update("authpass", values, selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 2:
                db.update("authphone", values, selection, selectionArgs);
                //	getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 3:
                db.update("ownerchange", values, selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 4:
                db.update("ischager", values, selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 5:
                db.update("islowbat", values, selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 6:
                db.update("pushfencing", values, selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 7:
                db.update("sosmsg", values, selection, selectionArgs);
                //getContext().getContentResolver().notifyChange(MSG_URI, null);
                break;
            case 8:
                db.update("kickuser", values, selection, selectionArgs);
                break;
            case 10:
                db.update("detachnotice", values, selection, selectionArgs);
                break;
            default:
                break;
        }
        return 0;
    }

}
